package com.guodi.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.guodi.common.constant.StatusCode;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.dto.MapCimSkylineViewportQueryParamDTO;
import com.guodi.map.entity.MapCimSkylineViewport;
import com.guodi.map.service.IMapCimSkylineViewportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @天际线控制器
 * @author:廖龙威
 * @since:2021-03-23
 */
@RestController
@RequestMapping("/MapCimSkylineViewport")
@Api(value = "天际线分析", tags = {"天际线分析"})
public class MapCimSkylineViewportController extends BaseController {

    @Autowired
    private IMapCimSkylineViewportService skylineViewportService;

    /**
     * 不分页查询
     *
     * @param skylineViewportQueryParamDTO 实体对象
     * @return 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "不分页查询")
    public R list(MapCimSkylineViewportQueryParamDTO skylineViewportQueryParamDTO) {
        LambdaQueryWrapper<MapCimSkylineViewport> wrapper = setQueryParam(skylineViewportQueryParamDTO);
        List<MapCimSkylineViewport> list = skylineViewportService.list(wrapper);
        return R.data(list);
    }

    /**
     * 设置查询参数
     *
     * @param skylineViewportQueryParamDTO 实体对象
     * @return 查询条件构造器
     */
    private LambdaQueryWrapper<MapCimSkylineViewport> setQueryParam(MapCimSkylineViewportQueryParamDTO skylineViewportQueryParamDTO) {
        LambdaQueryWrapper<MapCimSkylineViewport> wrapper = new QueryWrapper().lambda();
        if (StringUtils.isNotBlank(skylineViewportQueryParamDTO.getId())) {
            wrapper.eq(MapCimSkylineViewport::getId, skylineViewportQueryParamDTO.getId());
        }
        if (StringUtils.isNotBlank(skylineViewportQueryParamDTO.getName())) {
            wrapper.like(MapCimSkylineViewport::getName, skylineViewportQueryParamDTO.getName());
        }
        wrapper.orderByDesc(MapCimSkylineViewport::getCreateTime);
        return wrapper;
    }

    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 天际线
     */
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    public R<MapCimSkylineViewport> findById(String id) {
        if(StringUtils.isBlank(id)) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "id不能为空");
        }
        MapCimSkylineViewport skylineViewport = skylineViewportService.getById(id);
        return R.data(skylineViewport);
    }

    /**
     * 新增
     *
     * @param skylineViewport 实体对象
     * @return 成功则返回"新增成功"
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public R save(@RequestBody @Valid MapCimSkylineViewport skylineViewport) {
        if(StringUtils.isBlank(skylineViewport.getName())) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "天际线名称不能为空");
        }
        return skylineViewportService.saveSkylineViewport(skylineViewport, getUser().getUserId());
    }

    /**
     * 修改
     *
     * @param skylineViewport 实体对象
     * @return 成功则返回"修改成功"
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody @Valid MapCimSkylineViewport skylineViewport) {
        if(StringUtils.isBlank(skylineViewport.getId())) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "id不能为空");
        }
        if(StringUtils.isBlank(skylineViewport.getName())) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "天际线名称不能为空");
        }
        return skylineViewportService.updateSkylineViewport(skylineViewport, getUser().getUserId());
    }

    /**
     * 批量删除
     *
     * @param idList 主键集合
     * @return 成功则返回"删除成功"
     */
    @PostMapping("/delete")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "批量删除", notes = "List<String>")
    public R remove(@RequestBody List<String> idList) {
        if(idList == null || idList.size() ==0) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "idList不能为空");
        }
        skylineViewportService.removeByIds(idList);
        return R.success("批量删除成功");
    }

}